package dao;


import com.db.DBConn;

import model.Course;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Vector;

public class CourseDao {
    public Collection getDatas() throws SQLException {
        DBConn db = new DBConn();
        ResultSet rs = db.queryData("select * from course order by id");
        Collection col = new Vector();
        while (rs.next()) {
            Course course = new Course();
            course.setId(rs.getString("id"));
            course.setCno(rs.getString("cno"));
            course.setCname(rs.getString("cname"));
            course.setHour(rs.getString("hour"));
            course.setType(rs.getString("type"));
            col.add(course);
        }
        return col;
    }

    //提取教师信息by idc
    public Course getData(String id) throws SQLException {
        DBConn db = new DBConn();
        ResultSet rs = db.queryData("select * from course where id="+id);
        if(rs.next()) {
            Course course = new Course();
            course.setId(id);
            course.setCno(rs.getString("cno"));
            course.setCname(rs.getString("cname"));
            course.setHour(rs.getString("hour"));
            course.setType(rs.getString("type"));
            return course;
        }else {
            return null;
        }
    }

    //修改数据
    public void updateData(Course course) throws SQLException {
        DBConn db = new DBConn();
        //String ano = course.getAno();
        db.updateData("update course set cno='"+course.getCno()+"',cname='"+course.getCname()+"',hour='"+course.getHour()+"',type='"+course.getType()+"' where id="+course.getId());
    }

    //新增数据
    public void insertData(Course course) throws SQLException {
        DBConn db = new DBConn();
        db.updateData("insert into course(cno,cname,hour,type) values('"+course.getCno()+"','"+course.getCname()+"','"+course.getHour()+"','"+ course.getType()+"')");
    }

    //删除数据
    public void deleData(String id) throws SQLException {
        DBConn db = new DBConn();
        db.updateData("delete from course where id="+id);

    }

    public String getNameByNo(String cno) {
        DBConn db = new DBConn();
        try {
            ResultSet rs = db.queryData("select cname from course where cno='"+cno+"'");
            if (rs.next()) {
                String cname = rs.getString("cname");
                return cname;
            }else {
                return null;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
    public boolean getDataByCno(String cno,String cname){
        DBConn db = new DBConn();
        try{
            ResultSet rs = db.queryData("select * from course where cno='"+cno+"'and cname = '"+cname+"'");
            if(rs.next())
                return true;
            else
                return false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}